package com.campus.counseling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.counseling.entity.PlatformAccount;
import com.campus.counseling.model.mapper.PlatformAccountMapper;
import com.campus.counseling.model.dto.PlatformAccountDTO;
import com.campus.counseling.model.common.exception.ServiceException;
import com.campus.counseling.service.PlatformAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class PlatformAccountServiceImpl implements PlatformAccountService {

    private final PlatformAccountMapper platformAccountMapper;

    @Override
    public PlatformAccount getAccount() {
        return platformAccountMapper.selectOne(new LambdaQueryWrapper<>());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFeeRate(BigDecimal feeRate) {
        if (feeRate == null || feeRate.compareTo(BigDecimal.ZERO) < 0 || feeRate.compareTo(new BigDecimal("100")) > 0) {
            throw new ServiceException("费率必须在0-100之间");
        }
        
        PlatformAccount account = getAccount();
        if (account == null) {
            account = new PlatformAccount();
            account.setPlatformFeeRate(feeRate);
            platformAccountMapper.insert(account);
        } else {
            account.setPlatformFeeRate(feeRate);
            platformAccountMapper.updateById(account);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAccount(PlatformAccountDTO accountDTO) {
        PlatformAccount account = getAccount();
        if (account == null) {
            throw new ServiceException("平台账户不存在");
        }
        
        account.setTotalAmount(accountDTO.getTotalAmount());
        platformAccountMapper.updateById(account);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addIncome(BigDecimal amount) {
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new ServiceException("收入金额必须大于0");
        }
        
        PlatformAccount account = getAccount();
        if (account == null) {
            throw new ServiceException("平台账户不存在");
        }
        
        account.setTotalAmount(account.getTotalAmount().add(amount));
        platformAccountMapper.updateById(account);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deductAmount(BigDecimal amount) {
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new ServiceException("扣减金额必须大于0");
        }
        
        PlatformAccount account = getAccount();
        if (account == null) {
            throw new ServiceException("平台账户不存在");
        }

        platformAccountMapper.updateById(account);
    }

    @Override
    public Object getIncomeStats(String timeRange) {
        // TODO: 实现收入统计逻辑
        return null;
    }

    @Override
    public Object getExpenseStats(String timeRange) {
        // TODO: 实现支出统计逻辑
        return null;
    }

    @Override
    public Object getAccountFlow(Integer pageNum, Integer pageSize, String flowType) {
        // TODO: 实现资金流水查询逻辑
        return null;
    }
} 